<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <div id="test"></div>
  <script src="../js/react.development.js"></script>
  <script src="../js/react-dom.development.js"></script>
  <script src="../js/babel.min.js"></script>
  <script src="../js/prop-types.js"></script>
  <script type="text/babel">
    class MyComponent extends React.Component {
      // 创建ref容器
      myRef = React.createRef();
      // 创建state
      state = {
        opacity: 1
      }

      removeCom = () => {
        ReactDOM.unmountComponentAtNode(document.getElementById('test'));
      }
      // 1. 出生
      componentDidMount(){
        
        this.intervalId = setInterval(() => {
          let opacity = this.state.opacity - 0.1;
          if (opacity <= 0) {
            opacity = 1;
          }
          this.setState({
            opacity
          })
          
        }, 200);
      }

      // 2. 更新阶段
      componentWillUpdate(){
        console.log('componentDidMount')
      }
      componentDidUpdate(){
        console.log('componentDidUpdate')
      }

      // 3.死亡阶段
      componentWillUnmount(){
        console.log('componentWillUnmount');
        clearInterval(this.intervalId);
      }
      render () {
        return (
          <div>
            <p ref={this.myRef} style={{"opacity": this.state.opacity}}>你好，哈啊哈</p>
            <button onClick={this.removeCom}>点击消失</button>
          </div>
        )
      }
    }


    ReactDOM.render(<MyComponent />, document.getElementById('test'))
  </script>
</body>

</html>